package org.iteam.xlz.core.service.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.security.md5.Md5Util;
import org.iteam.core.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.log.LogAccount;
import org.iteam.xlz.core.entity.user.UserInfo;

public class DollarService extends BaseService<LogAccount> {
	static private DollarService instance;
	private StringBuilder sql=null;
	private List<Object> params=new ArrayList<Object>();
	
	private DollarService(){}
	
	public static DollarService getInstance(){
		if(null==instance){
			instance=new DollarService();
		}
		return instance;
	}
	
	public long counts(LogAccount model){
		params.clear();
		sql=new StringBuilder("select r.username as uid,l.amount,l.items,l.logtype,l.createtime from logaccount l left join userinfo r on l.uid=r.uid where 1=1");
		if(StringUtils.isNotBlank(model.getUid())){
			sql.append(" and r.username like ?");
			params.add("%"+model.getUid()+"%");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<LogAccount> query(LogAccount model,long page,long rows){
		params.clear();
		sql=new StringBuilder("select r.username as uid,l.amount,l.items,l.logtype,l.createtime from logaccount l left join userinfo r on l.uid=r.uid where 1=1");
		if(StringUtils.isNotBlank(model.getUid())){
			sql.append(" and r.username like ?");
			params.add("%"+model.getUid()+"%");
		}
		sql.append(" order by l.createtime desc limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<LogAccount>();
		}
	}
	
	public boolean update(LogAccount model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			UserInfo u=super.show(UserInfo.class,con,"select * from userinfo where mobile=?",model.getUid());
			if(null==u){
				return false;
			}
			int i=super.updates("update userinfo set daole=daole+? where uid=?",model.getAmount(),u.getUid());
			SqlBuilder sql=SqlBuilder.createInsert("logaccount");
			sql.addField("laid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sql.addField("uid",u.getUid());
			sql.addField("amount",model.getAmount());
			sql.addField("items",model.getItems());
			sql.addField("logtype",Float.parseFloat(model.getAmount())>0?1:0);
			sql.addField("createtime",DateUtil.currentTime());
			int j=super.updates(con,sql.getSql(),sql.getParams());
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
